﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileWatcher
{
    public partial class Form1 : Form

    {
        delegate void SetTextCallback(string text);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            FileSystemWatcher p = new FileSystemWatcher("d:/tools");
            p.EnableRaisingEvents = true;
            p.IncludeSubdirectories = true;
            p.Changed += new FileSystemEventHandler(p_Changed);
            p.Created += new FileSystemEventHandler(p_Created);
            p.Deleted += new FileSystemEventHandler(p_Deleted);
            p.Renamed += new RenamedEventHandler(p_Renamed);

        }

        private void p_Renamed(object sender, FileSystemEventArgs e)
        {
            string s = e.Name;

            SetText("【改名】" + DateTime.Now + s);
        }

        private void p_Changed(object sender, FileSystemEventArgs e)
        {
            string s = e.Name;

            SetText("【修改】" + DateTime.Now+s);
        }

        private void p_Created(object sender, FileSystemEventArgs e)
        {
            string s = e.Name;

            SetText("【创建】" + DateTime.Now + s);
        }

        private void p_Deleted(object sender, FileSystemEventArgs e)
        {
            string s = e.Name;

            SetText("【删除】" + DateTime.Now + s);
        }

        private void SetText(string text)
        {
            if (this.listBox1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.listBox1.Items.Add(text);
            }
        }

    }
}
